package gamecomm.activity;

import java.util.HashMap;


import android.app.ListActivity;

import gamecomm.library.Constants;
import gamecomm.library.DatabaseHandler;
import gamecomm.library.GamesDatabase;
import gamecomm.library.UserFunctions;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import android.view.View;

import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;

import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import android.widget.Toast;
import android.app.ListActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import static android.provider.BaseColumns._ID;


public class MyGameListActivity extends ListActivity implements OnClickListener, OnItemClickListener {  //EXTENDS LISTACTIVITY
	UserFunctions userFunctions;
	AutoCompleteTextView autoComplete;
	private TextView myrowid;

	private Button gameSearchButton;
	private Button BackButton;
	private ListView gamelistview; 
	 private GamesDatabase dblistdata;
	private static final String[] GAMES = new String[] {
        "Madden '13", "FIFA '13", "NBA '13", "MLB '13", "Need For Speed", 
         "Call Of Duty: MW2", "Call Of Duty: MW3", "Call Of Duty: Black Ops", 
        "Skyrim", "Halo", "Grand Theft Auto", "Assassins Creed", "Mortal Kombat", "PGA Tour", "StarCraft"
	};
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        userFunctions = new UserFunctions();
	        setContentView(R.layout.gamelist);  
	        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, GAMES);   
	        autoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
	        autoComplete.setThreshold(1);
	        autoComplete.setAdapter(adapter);
	        autoComplete.setOnItemClickListener(this);

	    	dblistdata = new GamesDatabase(this);
	       init();
	        registerForContextMenu(getListView());

	        showGames();  
	        
	        
	 }
	
		@Override
	 public void onItemClick(AdapterView<?> adapterView, View view,
				int arg2, long arg3) {
			// String item = ((TextView)view).getText().toString();
             
            // Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
             gameSearchButton.setEnabled(true);

	 }
	 public void init(){
		 gameSearchButton = (Button) this.findViewById(R.id.gameSearchButton);
		 gameSearchButton.setOnClickListener(this);
	        ListView gamelistview = getListView();
	       
	}
	
	public void onClick(View view) {
		String gameinfo = autoComplete.getText().toString();
		autoComplete.setText(""); //clear text box
		addGame(gameinfo);

    }
	        
	 
	      private void showGames(){
	    	  gameSearchButton.setEnabled(false);
	            Cursor cursor = getGames();
	        	String[] FROM = {Constants._ID, Constants.ITEM};
	        	int[] TO = {R.id.rowid, R.id.item};
	        	SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.list_item, cursor, FROM, TO);
	        	
	        	setListAdapter(sca);
	        	sca.notifyDataSetChanged();
	        }
	        
	        private Cursor getGames(){
	        	SQLiteDatabase db = dblistdata.getReadableDatabase();
	    		Cursor cursor = db.query(Constants.TABLE_NAME, null, null, null, null, null, null);
	    		startManagingCursor(cursor);
	    		return cursor;
	        }
	        
	        private void addGame(String gameinfo) {
	        	SQLiteDatabase db=dblistdata.getWritableDatabase();
	        	Context context = getApplicationContext();
	        	
	        	//gameinfo=getGameDetails(gameinfo);
	        		 ContentValues values = new ContentValues();
	        		 values.put(Constants.ITEM, gameinfo);
	        	db.insert(Constants.TABLE_NAME, null, values);
	        	db.close();
	    		getGames().requery(); //need this!!!
	    		showGames();  //need this!!!
	      
	        }
	      
	        
	    		 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	    		    	super.onCreateContextMenu(menu, v, menuInfo);
	    		    	menu.setHeaderTitle("Select an action");
	    		    	menu.add(0, v.getId(), 0, "Delete game");
	    		    	menu.add(1, v.getId(), 1, "Delete ALL games");
	    		    }
	    		    
	    		    @Override  
	    		    public boolean onContextItemSelected(MenuItem item) {  
	    		    	
	    		    	// Get the database _ID of the item that was deleted
	    		    	AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	    		    	View listItemView = info.targetView;
	    		    	TextView rowid = (TextView)listItemView.findViewById(R.id.rowid);	
	    		    	TextView description = (TextView)listItemView.findViewById(R.id.item);	
	    		    	String id = rowid.getText().toString();

	    		        if(item.getTitle() == "Delete game")
	    		        	deleteToDoItem(id);
	    		        else if (item.getTitle() == "Delete ALL games")
	    		        	resetTables();
	    		        else
	    		        	return false;  
	    		        return true;  
	    		    }  
	    		   private void deleteToDoItem(String itemId) {
	    		    	SQLiteDatabase db=dblistdata.getWritableDatabase();
	    		    	db.delete(Constants.TABLE_NAME, "_ID"+ "=" + itemId, null);
	    		    	this.closeContextMenu();
	    		    	getGames().requery();
	    		    	showGames();
	    			}
	    		  public void resetTables(){
	    		        SQLiteDatabase db = dblistdata.getWritableDatabase();
	    		       db.delete(Constants.TABLE_NAME, null, null);
	    		        //db.close();
	    		       this.closeContextMenu();
	    		    	getGames().requery();
	    		    	showGames();
	    		    }
			
	    		  

}       


